package org.dromara.system.controller.yzl;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.vo.ScheduleGanttQueryVo;
import org.dromara.system.utils.OssUtil;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.YzlScheduleFormationVo;
import org.dromara.system.domain.bo.YzlScheduleFormationBo;
import org.dromara.system.service.IYzlScheduleFormationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 计划编制
 *
 * @author wfy
 * @date 2025-09-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/scheduleFormation")
public class YzlScheduleFormationController extends BaseController {

    private final IYzlScheduleFormationService yzlScheduleFormationService;

    /**
     * 查询计划编制列表
     */
    //@SaCheckPermission("system:scheduleFormation:list")
    @GetMapping("/list")
    public TableDataInfo<YzlScheduleFormationVo> list(YzlScheduleFormationBo bo, PageQuery pageQuery) {
        return yzlScheduleFormationService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出计划编制列表
     */
    //@SaCheckPermission("system:scheduleFormation:export")
    @Log(title = "计划编制", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(YzlScheduleFormationBo bo, HttpServletResponse response) {
        List<YzlScheduleFormationVo> list = yzlScheduleFormationService.queryList(bo);
        ExcelUtil.exportExcel(list, "计划编制", YzlScheduleFormationVo.class, response);
    }

    /**
     * 获取计划编制详细信息
     *
     * @param id 主键
     */
    //@SaCheckPermission("system:scheduleFormation:query")
    @GetMapping("/{id}")
    public R<YzlScheduleFormationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(yzlScheduleFormationService.queryById(id));
    }

    /**
     * 新增计划编制
     */
    //@SaCheckPermission("system:scheduleFormation:add")
    @Log(title = "计划编制", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody YzlScheduleFormationBo bo) {
        bo.setDeptId(LoginHelper.getDeptId());
        bo.setUserId(LoginHelper.getUserId());
        return toAjax(yzlScheduleFormationService.insertByBo(bo));
    }

    /**
     * 文件上传
     */
    //@SaCheckPermission("system:scheduleFormation:add")
    @Log(title = "文件上传", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/addFile")
    public R<String> addFile(MultipartFile file, String fileName) throws Exception {
        String url =  OssUtil.uploadAliMulti(file,null,fileName);
        return R.ok("上传成功",url);
    }

    /**
     * 修改计划编制
     */
    //@SaCheckPermission("system:scheduleFormation:edit")
    @Log(title = "计划编制", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody YzlScheduleFormationBo bo) {
        return toAjax(yzlScheduleFormationService.updateByBo(bo));
    }

    /**
     * 删除计划编制
     *
     * @param ids 主键串
     */
    //@SaCheckPermission("system:scheduleFormation:remove")
    @Log(title = "计划编制", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(yzlScheduleFormationService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 甘特图
     *
     * @param YzlScheduleFormationBo
     * @return
     */
    @GetMapping("/ganttInfo")
    public R<List<ScheduleGanttQueryVo>> ganttInfo(YzlScheduleFormationBo bo, PageQuery pageQuery){
        pageQuery.setPageNum(1);
        pageQuery.setPageSize(99999);
        return R.ok(yzlScheduleFormationService.ganttInfo(bo, pageQuery));
    }


}
